<template>
	<view class="wrap">
		<view class="col">
			<view class="item">
				<view class="title">
					<image src="@/static/admin/icon/success.png" mode=""></image>
					已选择{{length}}位工人
				</view>
				<view class="name">
					{{name}}
				</view>
			</view>
		</view>
		<view class="col">
			<view class="item">
				<view class="title">
					<image src="@/static/admin/icon/success2.png" mode=""></image>
					请输入{{title}}数额
				</view>
				<input type="number" v-model="score" placeholder="请输入数额">
			</view>
		</view>
		<view class="col">
			<view class="item">
				<view class="title">
					<image src="@/static/admin/icon/success3.png" mode=""></image>
					请输入{{title}}原因
				</view>
				<textarea v-model="reason" placeholder="请输入原因"></textarea>
			</view>
		</view>
		<view class="submitBtn" @click="submit">
			提交
		</view>
		<uni-popup ref="popup">
			<view class="popup-content">
				<view class="num">
					{{score}}
				</view>
				<view class="tips">
					请确定为工人
					<text v-if="type == 'add'">加</text>
					<text v-else>减</text>
					安全积分{{score}}分？
				</view>
				<view class="btn">
					<view class="cancel" @click="close">
						取消
					</view>
					<view class="confirm" @click="confirm">
						确认
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				work_no: '',
				name: '',
				length: '',
				score: null,
				reason: null,
				title: null,
				type: null
			}
		},
		onLoad(option) {
			this.work_no = option.work_no
			this.name = option.name
			this.length = option.length
			this.type = option.type
			if (this.type == 'add') {
				this.title = "加分"
			} else {
				this.title = "减分"
			}
			uni.setNavigationBarTitle({
				title: this.title
			})
		},
		methods: {
			submit() {
				if (!this.score) {
					this.$toast("请输入分数")
				} else if (!this.reason) {
					this.$toast("请输入原因")
				} else {
					this.$refs.popup.open()
				}
			},
			confirm() {
				this.$post("/worker/score_operate", {
					work_no: this.work_no,
					type: this.type,
					score: this.score,
					reason: this.reason
				}).then(res => {
					this.$toast(res.msg)
					if (res.code == 200) {
						uni.redirectTo({
							url: `./success?type=${this.type}&score=${this.score}&length=${res.data.success}`
						})
					}
				})
			},
			close() {
				this.$refs.popup.close()
			}
		}
	}
</script>

<style lang="scss">
	.col {
		margin-top: 40rpx;

		.item {
			background: #EDF0F6;
			padding: 30rpx;
			border-radius: 16rpx;

			.title {
				font-size: 32rpx;
				color: $main-color;
				margin-bottom: 10rpx;

				image {
					width: 36rpx;
					height: 36rpx;
					margin-right: 10rpx;
					transform: translateY(6rpx);
				}
			}

			.name {
				font-size: 28rpx;
				line-height: 2;
			}
		}
	}

	.submitBtn {
		margin-top: 50rpx;
	}

	input,
	textarea {
		background: #FFFFFF;
		border: 1px solid #DADADA;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-top: 40rpx;
		font-size: 30rpx;
	}

	input {
		height: 90rpx;
		padding: 0 20rpx;
	}

	.popup-content {
		text-align: center;
		padding: 32rpx;
		border-radius: 20rpx;
		background-color: #fff;

		.num {
			color: #FF6262;
			font-size: 100rpx;
		}

		.tips {
			font-size: 32rpx;
			padding: 50rpx 0;
		}

		.btn {
			display: flex;
			justify-content: space-between;

			&>view {
				width: 250rpx;
				line-height: 88rpx;
				border-radius: 16rpx;
				color: #fff;
			}

			.cancel {
				background: #BEC0C5;
			}

			.confirm {
				background: $main-color;
			}
		}
	}
</style>